/*
* Yahtzee 3.0 Client
* (c) Karl Puusepp & Tony Lepmets
* maiz & co, 2012
*/

import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.StringTokenizer;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JOptionPane;


public class YahtzeeIO {
	
	BufferedReader in;
	PrintWriter out;
	
	YahtzeeIO(BufferedReader in, PrintWriter out) {	
		this.in = in;
		this.out=out;	
	}
	
	public void send(String cmd) {		
		this.out.println(cmd);
		this.out.flush();
	}
	
	StringTokenizer receive() throws IOException {
		StringTokenizer res = new StringTokenizer(in.readLine());
		if(res.countTokens() == 1)
			return null; // server never sends 1 token except for "STOP", when game is over.
						 // this null value is later checked in the loop in play() method
		return res;
	}
	
	String receiveString() throws IOException {
		
		return in.readLine();
	}
	
	public String askPlayerName(String a) {
		return JOptionPane.showInputDialog(
    			null, a, "Yahtzee 3.0",
    			JOptionPane.INFORMATION_MESSAGE);
	}
	
	protected JLabel createIconLabel(String path) {
		java.net.URL imgURL = getClass().getResource(path);
		try {
			ImageIcon icon = new ImageIcon(imgURL);
			return new JLabel(icon);
		} catch(NullPointerException e) {
			System.err.println("Ei leidnud pildifaili.");
			return null;
		}
	}
}
